module manager {

    export class BagManager extends egret.EventDispatcher {

        private bagItems: Array<BagItem> = [];

        public static readonly GET_USER_INFO_COMPLETE: string = "get_user_info_complete";

        public constructor() {
            super();
        }

        public static readonly RELIVE_PROP_ID: number = 4;

        public getReliveCount(): number {
            return this.reliveCount;
        }

        private reliveCount: number = 0;

        private had_invited = false;

        public getMyBag() {
            egret.log("开始获取session状态");
            if (this.had_invited) return;
            this.had_invited = true;
            let that = this;
            let request = new egret.HttpRequest();
            request.responseType = egret.HttpResponseType.TEXT;
            let url = StatisticsManager.domain
                + '/index.php?g=WxaGame&m=Invite&a=today_invite_record&session_3rd='
                + manager.GameManager.getInstance().session_3rd;
            egret.log("获取session url:", url);
            request.open(url, egret.HttpMethod.GET);
            request.send();
            request.addEventListener(egret.Event.COMPLETE, function (event) {
                let request = <egret.HttpRequest>event.currentTarget;
                that.syncBagItems(request);
            }, this);

            request.addEventListener(egret.IOErrorEvent.IO_ERROR, this.onPostIOError, this);
        }

        private syncBagItems(request: egret.HttpRequest): void {
            let data = JSON.parse(request.response);
            egret.log("获取用户背包数据:", data);
            if (data.data != null) {
                let that = this;
                that.bagItems = data.data;
                let len = that.bagItems.length;
                for (let i = 0; i < len; i++) {
                    let item = that.bagItems[i];
                    if (parseInt(item.props_id) == BagManager.RELIVE_PROP_ID) {
                        that.reliveCount++;
                    }
                    if (item.portrait != null && item.portrait.length < 5) {
                        item.portrait = "https://wx.qlogo.cn/mmopen/vi_32/uONskOLwR9tCZVQZ45x2zXDxSDFA5YcgR3cnWQBMY9LPNrJWpwibCCK0X8N9I8xB5tkl5grmwIpjYhBqA1OaZug/132";
                    }
                }
            }

            let userSetting = manager.GameManager.getInstance().getUserSetting();
            let currentDayNumber = manager.UtilsManager.getYearMonthDayNumber();
            //如果没有复活次数，本地立刻重置昨日数据
            if (this.reliveCount < 1) {
                userSetting.lastDay = currentDayNumber;
                userSetting.reliveTimes = 0;
                userSetting.save();
            } else {
                let lastDayNumber = userSetting.lastDay;
                if (lastDayNumber < currentDayNumber) {
                    userSetting.lastDay = currentDayNumber;
                    userSetting.reliveTimes = 0;
                    userSetting.save();
                }
            }
        }

        private todayLiveTimes: number = 0;

        public relive(): void {
            this.todayLiveTimes++;
        }

        private onPostIOError(eve: egret.IOErrorEvent): void {
            wx.showToast("获取用户背包数据失败");
        }


        public getItems(): Array<BagItem> {
            return this.bagItems;
        }

    }

    export class BagItem {
        public portrait: string;
        public wechaname: string;
        public props_id: string;
    }

}